package com.gec.system.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gec.model.system.ITripArea;
import com.gec.model.system.ITripItinerary;
import com.gec.model.vo.ITripAreaQueryVo;
import com.gec.system.common.Result;
import com.gec.system.service.ITripAreaService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 省份表 前端控制器
 * </p>
 *
 * @author chenzhiling
 * @since 2024-01-22
 */
@RestController
@RequestMapping("/system/itrArea")
@Api(tags="区域管理")
@CrossOrigin
public class ITripAreaController {
    @Autowired
    private ITripAreaService iTripAreaService;


    @ApiOperation("获取全部评论列表")

    @GetMapping("/findAll")
    public Result findAll()
    {
        List<ITripArea> list = this.iTripAreaService.list();
        return Result.ok(list);
    }




    @ApiOperation("根据id去移除一个角色")
    // 测试删除
    @DeleteMapping("/removeArea/{id}")
    public Result removeArea(@PathVariable Long id)
    {
        boolean b = this.iTripAreaService.removeById(id);
        if (b)
        {
            return Result.ok();
        }
        else
        {
            return Result.fail();
        }
    }
    // 分页和条件查询
    @GetMapping("/{page}/{limit}")
    public Result findAreaByPageQuery(@PathVariable Long page,
                                      @PathVariable Long limit,
                                      ITripAreaQueryVo iTripAreaQueryVo)
    {
        //1.创建分页对象
        IPage<ITripArea> p1 = new Page<>(page,limit);
        //2.调用方法
        p1 =    this.iTripAreaService.selectPage(p1,iTripAreaQueryVo);
        //3.返回
        return Result.ok(p1);
    }
    // 添加角色

    @PostMapping("/addArea")
    public Result addArea(@RequestBody ITripArea iTripArea)
    {
        //        添加编号唯一性判断
        ITripArea it = iTripAreaService.selectByName(iTripArea.getAreaName());
        if (it != null) {
            return Result.fail("name重复");
        }
        boolean res = this.iTripAreaService.save(iTripArea);
        if (res)
        {
            return Result.ok();
        }
        else
        {
            return Result.fail();
        }
    }
    // 修改

    @GetMapping("/findAreaById/{id}")
    public Result findAreaById(@PathVariable Long id)
    {
        ITripArea iTripArea = this.iTripAreaService.getById(id);
        return Result.ok(iTripArea);
    }



    // 实现修改
    @PostMapping("/updateArea")
    public Result updateArea(@RequestBody ITripArea iTripArea)
    {
        boolean b = this.iTripAreaService.updateById(iTripArea);
        if (b)
        {
            return Result.ok();
        }
        else
        {
            return Result.fail();
        }
    }
    // 批量删除
    @DeleteMapping("/removeAreaByIds")
    public Result removeAreaByIds(@RequestBody List<Long> ids)
    {
        boolean b = this.iTripAreaService.removeByIds(ids);
        if (b)
        {
            return Result.ok();
        }
        else
        {
            return Result.fail();
        }
    }
}

